package cn.edu.scau.cmi.liangzaoqing.client;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.liangzaoqing.hibernate.dao.MajorDAO;
import cn.edu.scau.cmi.liangzaoqing.hibernate.domain.Major;
import cn.edu.scau.cmi.liangzaoqing.util.ApplicationContextUtil;
import cn.edu.scau.cmi.liangzaoqing.util.HibernateSessionFactoryUtil;

/*
 * 例子6中的DAO对象和DAO所依赖的HibernateSessionFactoryUtil对象需要手工维护，
 * 在业务逻辑中内聚了不应该的功能，因此，采用DI的方式，将这些对象由DI的方式进行管理*/
public class HibernateClient7HibernateSpring {

	public static void main(String[] args) {
//		获取单例的applicationContext对象
		ApplicationContext applicationContext = ApplicationContextUtil.getApplicationContext();
		MajorDAO majorDAO = (MajorDAO) applicationContext.getBean("majorDAO");
		
		Major major=new Major();
		major.setName("Spring + Hibernate");
		major.setId(7L);
		
		majorDAO.save(major);
 		
		List<Major> majors = majorDAO.findAll();
		
		for (Iterator iterator = majors.iterator(); iterator.hasNext();) {
			Major object = (Major) iterator.next();
			System.out.println(object.getName());
		}
	}
}